//
//  PhotoViewController.m
//  PhotoViewer
//
//  Created by Ray Wenderlich on 6/30/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "PhotoViewController.h"
#import "PhotoSet.h"

@implementation PhotoViewController
@synthesize photoSet = _photoSet;
@synthesize title  = title;
@synthesize babyPhotos;
@synthesize photoArray;

- (void) viewDidLoad {

	[super viewDidLoad];
	self.title = NSLocalizedString(@"Album", @"");
	Dao *dao = [Dao getInstance];
	NSString *whereClause = @"ORDER BY TIMESTAMP DESC";
	self.babyPhotos = [[NSMutableArray alloc]init];
	self.photoArray = [[NSMutableArray alloc]init];
	[dao QueryBabyPhotoEntryWith:whereClause records:self.babyPhotos];
	
	/* Not autoreleased */
	//NSArray *array = [[NSArray alloc] initWithArray:mutableArray];
	/* Autoreleased array */
	//NSArray *array = [NSArray arrayWithArray:mutableArray];
	for(int i = 0; i < [self.babyPhotos count]; i++)
	{        
		BabyPhotoEntry *entry = [self.babyPhotos objectAtIndex:i];
		MockPhoto *pv = [[[MockPhoto alloc]
						  initWithURL:[NSString stringWithFormat:@"%@%@",@"documents://",entry.photoPath]
						  smallURL:[NSString stringWithFormat:@"%@%@",@"documents://",entry.thumPath] 
						  size:CGSizeMake(320, 320)
						  caption:[entry getSummaryString]] autorelease];
		
		[photoArray addObject:pv];
	}
	
	self.photoSource = [[MockPhotoSource alloc]
						initWithType:MockPhotoSourceNormal
						title:NSLocalizedString(@"Baby Album", @"")
						photos:self.photoArray
						photos2:self.photoArray];
	
	UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"Edit", @"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleShare)		
								 ];
	self.navigationItem.leftBarButtonItem = rightItem;
	[rightItem release];
	
	
/*	
	self.photoSource = [[MockPhotoSource alloc]
						initWithType:MockPhotoSourceNormal
						title:@"Flickr Photos"
						photos:[[NSArray alloc] initWithObjects:
								[[[MockPhoto alloc]
								  initWithURL:@"http://farm4.static.flickr.com/3246/2957580101_33c799fc09_o.jpg"
								  smallURL:@"http://farm4.static.flickr.com/3246/2957580101_d63ef56b15_t.jpg"
								  size:CGSizeMake(960, 1280)] autorelease],
								[[[MockPhoto alloc]
								  initWithURL:@"http://farm4.static.flickr.com/3118/3122869991_c15255d889.jpg"
								  smallURL:@"http://farm4.static.flickr.com/3118/3122869991_c15255d889_t.jpg"
								  size:CGSizeMake(500, 406)] autorelease],
								
								nil
								]
						
						photos2:nil
						];
 */
}

-(void)toggleShare{

}

-(void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
}


- (void) dealloc {
    self.photoSet = nil;    
    [super dealloc];
}

- (NSInteger)numberOfPhotos {
    return self.photoArray.count;
}

- (NSInteger)maxPhotoIndex {
    return self.photoArray.count-1;
}

- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex {
    if (photoIndex < self.photoArray.count) {
        return [self.photoArray objectAtIndex:photoIndex];
    } else {
        return nil;
    }
}

#pragma mark TTModel

- (BOOL)isLoading { 
    return FALSE;
}

- (BOOL)isLoaded {
    return TRUE;
}

@end